//
//  WXMUIViewLieExtension.swift
//  IM_Client_Swift
//
//  Created by wq on 2020/5/25.
//  Copyright © 2020 wq. All rights reserved.
//
import UIKit
import Foundation
import SnapKit

private var onceTap :Void?
private var doubleTap :Void?
extension UIView {

    /**< 在window中的Rect  */
    func locationRectWithWindow_k() -> CGRect {
        guard let window = UIApplication.shared.keyWindow else { return .zero }
        return convert(bounds, to: window)
    }

    /**< 在window中的Point */
    func locationCenterWithWindow_k() -> CGPoint {
        let rect = locationRectWithWindow_k()
        let x = rect.origin.x + (rect.size.width / 2.0)
        let y = rect.origin.y + (rect.size.height / 2.0)
        return CGPoint(x: x, y: y)
    }

    /**< 单击Selector  */
    func tappedWithTarget_k(target: Any, selector: Selector) {
        let gesture = UITapGestureRecognizer(target: target, action: selector)
        gesture.delegate = self as? UIGestureRecognizerDelegate
        gesture.numberOfTapsRequired = 1
        gesture.numberOfTouchesRequired = 1

        isUserInteractionEnabled = true
        addGestureRecognizer(gesture)
    }

//    /**< 截图 */
    func makeImage_k() -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(frame.size, false, UIScreen.main.scale)
        layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image
    }
//	
//	/**< 截图 */
	func makeWKViewImage_k(sharedWebView : UIView) -> UIImage? {
		let size = CGSize(width: sharedWebView.bounds.width, height: sharedWebView.bounds.height)
		UIGraphicsBeginImageContextWithOptions(size,false, 0)
		sharedWebView.layer.render(in: UIGraphicsGetCurrentContext()!)
		sharedWebView .drawHierarchy(in: sharedWebView.bounds, afterScreenUpdates: true)
		let uiImage = UIGraphicsGetImageFromCurrentImageContext()!
		UIGraphicsEndImageContext()
		return uiImage
	}

    /**< 单击block  */
    @discardableResult
    func setOneTappedWithCallback_k(callback: @escaping () -> ()) -> UITapGestureRecognizer {
        let gesture = addTapGesture(taps: 1, touches: 1, selector: #selector(tappedEvent(gesture:)))
        objc_setAssociatedObject(self, &onceTap, callback, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        return gesture
    }
//
//    /**< 双击block  */
//    @discardableResult
//    func setDoubleTappedWithCallback_k(callback: @escaping () -> ()) -> UITapGestureRecognizer {
//        let gesture = addTapGesture(taps: 2, touches: 1, selector: #selector(tappedEvent(gesture:)))
//        objc_setAssociatedObject(self, &doubleTap, callback, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
//        return gesture
//    }

//    /**< 上下居中 */
//    func venicalSet_k(above: UIView?, nether: UIView?, interval: CGFloat) {
//        if (above == nil || nether == nil || self.frame.size.height == 0) { return }
//
//        let totalHeight = self.frame.size.height
//        let totalInterval = totalHeight - above!.frame.size.height - nether!.frame.size.height
//        let topAbove = (totalInterval - interval) / 2.0
//
//        var rectAbove = above!.frame
//        rectAbove.origin.y = topAbove
//        above!.frame = rectAbove
//
//        var rectNether = nether!.frame
//        rectNether.origin.y = totalHeight - topAbove - nether!.frame.size.height
//        nether!.frame = rectNether
//    }
//    
//    /**< 左右居中 */
//    func horizontalSet_k(above: UIView?, nether: UIView?, interval: CGFloat) {
//        if (above == nil || nether == nil || self.frame.size.height == 0) { return }
//
//        let totalWidth = self.frame.size.width
//        let totalInterval = totalWidth - above!.frame.size.width - nether!.frame.size.width
//        let leftAbove = (totalInterval - interval) / 2.0
//
//        var rectAbove = above!.frame
//        rectAbove.origin.x = leftAbove
//        above!.frame = rectAbove
//
//        var rectNether = nether!.frame
//        rectNether.origin.x = totalWidth - leftAbove - nether!.frame.size.width
//        nether!.frame = rectNether
//    }

    /**< 渐现动画 */
    func fadeAnimation_k(duration: CGFloat = 0.5) {
        let transition = CATransition()
        transition.duration = CFTimeInterval(duration)
        transition.type = .fade
        transition.subtype = .fromRight
        self.layer.add(transition, forKey: "fadeAnimation")
    }
	
	func animationHide(duration: CGFloat = 0.3)  {
		UIView.animate(withDuration: duration) {
			self.alpha = 0
		} completion: { _ in
			self.alpha = 1
			self.isHidden = true
		}
	}

//    /**< 翻页 */
//    func moveInAnimation_k(duration: CGFloat = 0.35) {
//        let transition = CATransition()
//        transition.duration = CFTimeInterval(duration)
//        transition.type = .moveIn
//        transition.subtype = .fromTop
//		transition.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
//        self.layer.add(transition, forKey: "pageCurl")
//    }
//	
//	/**< 翻页 */
//	func moveInAnimation2222_k(duration: CGFloat = 0.35) {
//		let transition = CATransition()
//		transition.duration = CFTimeInterval(duration)
//		transition.type = .moveIn
//		transition.subtype = .fromRight
//		transition.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
//		self.layer.add(transition, forKey: "pageCurl")
//	}
//	
//	/**< 翻页 */
//	func moveInAnimation3333_k(duration: CGFloat = 0.35) {
//		let transition = CATransition()
//		transition.duration = CFTimeInterval(duration)
//		transition.type = .moveIn
//		transition.subtype = .fromBottom
//		transition.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
//		self.layer.add(transition, forKey: "pageCurl")
//	}
//
//    /**< 翻页 */
//    func pageCurlAnimation_k(duration: CGFloat = 0.5) {
//        let transition = CATransition()
//        transition.duration = CFTimeInterval(duration)
//        transition.type = CATransitionType(rawValue: "pageCurl")
//        /**< transition.type = CATransitionType(rawValue: "pageUnCurl") */
//        /**< transition.type = CATransitionType(rawValue: "oglFlip") */
//        transition.subtype = .fromLeft
//		
//        self.layer.add(transition, forKey: "pageCurl")
//    }
//    
//    /**< 消失动画  */
//    func disappearAnimation_k(duration: CGFloat = 0.5) {
//        UIView.animate(withDuration: TimeInterval(duration), animations: {
//            self.alpha = 0
//        }) {
//            self.alpha = 1
//            $0 ? self.removeFromSuperview() : nil
//        }
//    }
//  
//    *< 点击事件  
    @objc fileprivate func tappedEvent(gesture: UITapGestureRecognizer) {
        var callback: (() -> ())? = nil
        if gesture.numberOfTapsRequired == 1 {
            callback = objc_getAssociatedObject(self, &onceTap) as? (() -> ())
        }

		
        if gesture.numberOfTapsRequired == 2 {
            callback = objc_getAssociatedObject(self, &doubleTap) as? (() -> ())
        }

        if callback != nil { callback!() }
    }
//
    /**< 实例化手势  */
    fileprivate func addTapGesture(taps: Int, touches: Int, selector: Selector) -> UITapGestureRecognizer {
        let gesture = UITapGestureRecognizer(target: self, action: selector)
        gesture.delegate = self as? UIGestureRecognizerDelegate
        gesture.numberOfTapsRequired = taps
        gesture.numberOfTouchesRequired = touches

        isUserInteractionEnabled = true
        addGestureRecognizer(gesture)
        return gesture
    }

//    /**< xib  */
//    class func xib() -> UIView? {
//        guard let className = String(NSStringFromClass(self)).components(separatedBy: ".").last else {
//            return nil
//        }
//        let nib = UINib(nibName: className, bundle: nil)
//        return nib.instantiate(withOwner: nil, options: nil).first as? UIView
//    }
//	
//	
//	/**< xib  */
//	class func xib(_ index: Int) -> UIView? {
//		guard let className = String(NSStringFromClass(self)).components(separatedBy: ".").last else {
//			return nil
//		}
//		let nib = UINib(nibName: className, bundle: nil)
//		return nib.instantiate(withOwner: nil, options: nil)[index] as? UIView
//	}
//	
//	
//	/**< xib  */
//	class func xib(_ fileName: String, _ index: Int) -> UIView? {
//		let nib = UINib(nibName: fileName, bundle: nil)
//		return nib.instantiate(withOwner: nil, options: nil)[index] as? UIView
//	}
//
//    /**< 任意角画圆  */
//    func drawSemicircleRectCorner(rectCorner: UIRectCorner, cornerRadius: CGFloat) {
//        let size = CGSize(width: cornerRadius, height: cornerRadius)
//        let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: rectCorner, cornerRadii: size)
//        let maskLayer = CAShapeLayer()
//        maskLayer.frame = bounds
//        maskLayer.path = maskPath.cgPath
//        layer.mask = maskLayer
//    }
//
//    /**< 圆角边框  */
//    func drawSemicircleRectCorner(cornerRadius: CGFloat, lineColor: UIColor) {
//        let size = CGSize(width: cornerRadius, height: cornerRadius)
//        let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: .allCorners, cornerRadii: size)
//        let maskLayer = CAShapeLayer()
//        maskLayer.frame = bounds
//        maskLayer.path = maskPath.cgPath
//        layer.mask = maskLayer
//    }

    
    
 
}
